﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 事件映射管理器
/// 用途：有些事件，只需要监听某个id，所以不需要每次触发的时候给所以的监听者发送通知，而是只给有相应id的监听者发送通知。
/// 
/// </summary>
/// <typeparam name="ID"> 泛型，可以为int，string等</typeparam>
/// <typeparam name="Param"> 泛型， （代理delegate）回调函数的参数 </typeparam>
public class EventDictionary<ID, Param> 
{
    public static EventDictionary<ID, Param> Create()
    {
        return new EventDictionary<ID, Param>();
    }

    EventDictionary()
    {
        mBuffer = new Dictionary<ID,System.Action<Param>>();
    }

    Dictionary<ID, System.Action<Param>> mBuffer;

    public void RegistEvent(ID id, System.Action<Param> act)
    {
        if (act == null)
            return;

        if (mBuffer.ContainsKey(id))
            mBuffer[id] += act;
        else
            mBuffer.Add(id, act);
    }

    public void RemoveEvent(ID id, System.Action<Param> act)
    {
        if (mBuffer.ContainsKey(id))
        {
            mBuffer[id] -= act;
            if (mBuffer[id] == null)
                mBuffer.Remove(id);
        }
    }

    public void Clear(ID id)
    {
        if (mBuffer.ContainsKey(id))
        {
            mBuffer.Remove(id);
        }
    }

    public void Clear()
    {
        mBuffer.Clear();
    }

    public void Execute(ID id, Param p)
    {
        if (mBuffer.ContainsKey(id))
            mBuffer[id](p);
    }

    public void ExecuteAndRemove(ID id, Param p)
    {
        if (mBuffer.ContainsKey(id))
        {
            var tmp = mBuffer[id];
            mBuffer.Remove(id);
            tmp(p);
        }
    }
}

public class EventDictionary<ID, T1, T2>
{
    public static EventDictionary<ID, T1, T2> Create()
    {
        return new EventDictionary<ID, T1, T2>();
    }

    EventDictionary()
    {
        mBuffer = new Dictionary<ID, System.Action<ID, T1, T2>>();
    }

    Dictionary<ID, System.Action<ID, T1, T2>> mBuffer;

    public void RegistEvent(ID id, System.Action<ID, T1, T2> act)
    {
        if (act == null)
            return;

        if (mBuffer.ContainsKey(id))
            mBuffer[id] += act;
        else
            mBuffer.Add(id, act);
    }

    public void RemoveEvent(ID id, System.Action<ID, T1, T2> act)
    {
        if (mBuffer.ContainsKey(id))
        {
            mBuffer[id] -= act;
            if (mBuffer[id] == null)
                mBuffer.Remove(id);
        }
    }

    public void Clear(ID id)
    {
        if (mBuffer.ContainsKey(id))
        {
            mBuffer.Remove(id);
        }
    }

    public void Clear()
    {
        mBuffer.Clear();
    }

    public void Execute(ID id, T1 p1, T2 p2)
    {
        if (mBuffer.ContainsKey(id))
            mBuffer[id](id, p1, p2);
    }

    public void ExecuteAndRemove(ID id, T1 p1, T2 p2)
    {
        if (mBuffer.ContainsKey(id))
        {
            var tmp = mBuffer[id];
            mBuffer.Remove(id);
            tmp(id, p1, p2);
        }
    }
}

public class EventDictionary<ID>
{
    public static EventDictionary<ID> Create()
    {
        return new EventDictionary<ID>();
    }

    EventDictionary()
    {
        mBuffer = new Dictionary<ID, System.Action>();
    }

    Dictionary<ID, System.Action> mBuffer;

    public void RegistEvent(ID id, System.Action act)
    {
        if (mBuffer.ContainsKey(id))
            mBuffer[id] += act;
        else
            mBuffer.Add(id, act);
    }

    public void RemoveEvent(ID id, System.Action act)
    {
        if (mBuffer.ContainsKey(id))
        {
            mBuffer[id] -= act;
            if (mBuffer[id] == null)
                mBuffer.Remove(id);
        }
    }

    public void Execute(ID id)
    {
        if (mBuffer.ContainsKey(id))
            mBuffer[id]();
    }
}